package com.zx.kitchen.index.common.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.adapter.GoodsReportDetailAdapter;
import com.zx.kitchen.index.common.entity.GoodDetailListBean;
import com.zx.kitchen.index.common.mvp.presenter.BusinessGoodsDetailPresenter;
import com.zx.kitchen.index.common.mvp.presenter.PurchaseGoodsDetailPresenter;
import com.zx.kitchen.index.common.mvp.view.GoodsDetailView;
import com.zx.kitchen.index.common.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */

public class ReportGoodsDetailActivity extends Activity implements View.OnClickListener, GoodsDetailView {
    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private Button btnChart;
    private ListView listGoodsDetail;
    private boolean isPurchase = false;
    private String goods_id;
    private String goods_name;
    private EditText etStartTime;
    private EditText etEndTime;
    private Spinner spDate;
    private String[] mFontArrayString = new String[]{};
    private ArrayAdapter<String> mArrayAdapter;
    private String reportType = "d";
    private BusinessGoodsDetailPresenter businessGoodsDetailPresenter;
    private PurchaseGoodsDetailPresenter purchaseGoodsDetailPresenter;
    private String startTime = "";
    private String endTime = "";
    private GoodsReportDetailAdapter goodsReportDetailAdapter;
    private List<GoodDetailListBean> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_goods_detail);
        goods_id = getIntent().getStringExtra("goods_id");
        goods_name = getIntent().getStringExtra("goods_name");

        initView();
        setListener();
    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        toolbarTitle.setTextColor(getResources().getColor(R.color.blue));
        toolbarTitle.setText(goods_name);
        listGoodsDetail = (ListView) findViewById(R.id.list_business_goods_detail);
        btnChart = (Button) findViewById(R.id.btn_chart1);
        View headerView1 = LayoutInflater.from(this).inflate(R.layout.header_select_date, null);
        View headerView2 = LayoutInflater.from(this).inflate(R.layout.header_spinner_date, null);
        listGoodsDetail.addHeaderView(headerView2);
        listGoodsDetail.addHeaderView(headerView1);
        etStartTime = (EditText) headerView1.findViewById(R.id.et_start_time);
        etEndTime = (EditText) headerView1.findViewById(R.id.et_end_time);
        spDate = (Spinner) headerView2.findViewById(R.id.sp_date);

        etStartTime.setText(TimeUtil.getToday());
        etEndTime.setText(TimeUtil.getToday());

        mFontArrayString = new String[]{"日", "周", "月"};
        mArrayAdapter = new ArrayAdapter<String>(this, R.layout.edit_spinner, mFontArrayString) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.edit_spinner_item, parent, false);
                }
                TextView spinnerText = (TextView) convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
                return convertView;
            }
        };
        spDate.setAdapter(mArrayAdapter);

        goodsReportDetailAdapter = new GoodsReportDetailAdapter(this, mList);
        listGoodsDetail.setAdapter(goodsReportDetailAdapter);
        businessGoodsDetailPresenter = new BusinessGoodsDetailPresenter(getBaseContext(), this);
        purchaseGoodsDetailPresenter = new PurchaseGoodsDetailPresenter(getBaseContext(), this);


    }

    private void setListener() {
        toolbarLeft.setOnClickListener(this);
        btnChart.setOnClickListener(this);

        spDate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        reportType = "d";//d
                        if (isPurchase) {
                            purchaseGoodsDetailPresenter.getFromNet(startTime, endTime, goods_id, reportType);
                        } else {
                            businessGoodsDetailPresenter.getFromNet(startTime, endTime, goods_id, reportType);
                        }

                        break;
                    case 1:
                        reportType = "w";//w
                        if (isPurchase) {
                            purchaseGoodsDetailPresenter.getFromNet(startTime, endTime, goods_id, reportType);
                        } else {
                            businessGoodsDetailPresenter.getFromNet(startTime, endTime, goods_id, reportType);
                        }
                        break;
                    case 2:
                        reportType = "m";//y
                        if (isPurchase) {
                            purchaseGoodsDetailPresenter.getFromNet(startTime, endTime, goods_id, reportType);
                        } else {
                            businessGoodsDetailPresenter.getFromNet(startTime, endTime, goods_id, reportType);
                        }
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        listGoodsDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(ReportGoodsDetailActivity.this, GoodsDayDetailActivity.class)
                        .putExtra("day", mList.get(position - 2).getDetail().get(position - 2).getD())
                        .putExtra("goods_id", goods_id)
                        .putExtra("goods_name", goods_name));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftLin:
                finish();
                break;
            case R.id.btn_chart1:
                startActivity(new Intent(ReportGoodsDetailActivity.this, ChartGoodsActivity.class)
                        .putExtra("reportType", reportType)
                        .putExtra("isPurchase", isPurchase)
                        .putExtra("goods_id", goods_id)
                        .putExtra("goods_name", goods_name));
                break;
        }
    }

    @Override
    public void onSuccess(GoodDetailListBean goodDetailListBean) {
        mList.clear();
        mList.add(goodDetailListBean);
        goodsReportDetailAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }
}

